﻿var startno = 2;
var endno = 10;

//题型枚举
var QuestionCategoryOption = {
    Choice: 2,  // 选择题
    Judge: 3,   // 判断题
    FITBs: 4,   // 填空题
    SAQs: 5     // 简答题
}

//alert(new Date());

function InitItemCountSlt(target, defaultvalue) {
    var resContainer = document.getElementById(target);
    resContainer.length = 0;
    //  resContainer.options.add(new Option("请选择课程", "-1"));
    for (var i = startno; i <= endno; i++) {
        resContainer.options.add(new Option(i, i));
    }
    resContainer.value = defaultvalue;
}

//设置题型
function SetQuestionCategory(v) {
    var Container = document.getElementById("answerDiv");
    var defaultScore = $("#CategorySlt").find("option:selected").attr("title");
    $("#ScoreTxt").attr("value", defaultScore);
    if (v == QuestionCategoryOption.FITBs || v == QuestionCategoryOption.SAQs) {
        Container.style.display = "none";
        //InitItemCountSlt("ItemCountSlt", 2);
        //SetItemCount(0);
    }
    else {
        Container.style.display = "";
    }
}


//设置选项
//根据hidden 在新的DIV中 输出数据
function SetItemCount(v) {
    $("#NewItem").html("");
    var html = "";
    for (var i = 0; i < v; i++) {
        html += OptionTemplateWithinID(i);
    }
    $("#NewItem").html(html);
    InitItemData(v);
    InitCheckBoxData(v);
}

//设置数据
//如果下拉列表数据小于原有数据量
//刚根据下拉列表的数据设置
function InitItemData(v) {
    var count = parseInt($("#datacount").val());
    count = count >= v ? v : count;
    for (var i = 0; i < count; i++) {
        $("#option_" + i).val($("#title_" + i).val());
    }
}

//设置复选框
//具体操作过程与选项类似
//在新的DIV中 输出数据
//再根据下拉列表与原有数据量 设置复选框数据
function InitCheckBoxData(v) {
    var html = "";
    for (var i = 0; i < v; i++) {
        html += CheckBoxTemplate(i);
    }
    $("#CorrectChbList").html(html);

    var count = parseInt($("#datacount").val());
    count = count >= v ? v : count;
    for (var i = 0; i < count; i++) {
        var correct_value = $("#correct_" + i).val();
        if (correct_value == "1") {
            $("#chb_" + i)[0].checked = true;
        }
    }
}

function InitAreaData() {
    var a = $("#area_v").val();
    var areaList = document.getElementsByName("AreaChb");
    for (var i = 0; i < areaList.length; i++) {
        var v = $("#AreaChb_" + i).val();
        //    alert(v);
        if (a.indexOf(v) != -1) {
            $("#AreaChb_" + i)[0].checked = true;
        }
    }
}

function OptionTemplateWithinID(index) {
    var html = "";

    html += "<div id=\"item_" + index + "\">\r\n";
    html += OptionTemplateWithoutID(index);
    html += "</div>\r\n";

    return html;
}

function OptionTemplateWithoutID(index) {
    var html = "";

    html += "  <div class=\"box_1\">\r\n";
    html += "    <div class=\"fl tr left\">&nbsp;</div>\r\n";
    html += "    <div class=\"fl left_2\"><span id=\"Index_" + index + "\">" + OptionIndex[index] + "</span>&nbsp;</div>\r\n";
    html += "    <div class=\"fl\"><input type=\"text\" id=\"option_" + index + "\" name=\"option_" + index + "\" style=\"width:400px;\" /></div>\r\n";
    html += "  </div>\r\n";

    return html;
}

function CheckBoxTemplate(index) {
    var html = "";
    html += "  <div class=\"fl left_2\">&nbsp;" + OptionIndex[index] + "&nbsp;</div><input type=\"checkbox\" id=\"chb_" + index + "\" name=\"chblist\" class=\"fl c_chb\" />\r\n";
    return html;
}


var OptionIndex = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

function CheckForm() {
    var count = $("#ItemCountSlt").val();

    if ($("#CategorySlt").val() == QuestionCategoryOption.FITBs || $("#CategorySlt").val() == QuestionCategoryOption.SAQs) {
        count = -1;
    }

    var err = false;
    for (var i = 0; i < count; i++) {
        if ($("#option_" + i).val() == "") {
            alert("选项不能为空");
            err = true;
            break;
        }
    }

    var title = $("#TitleTxt").val();
    var category = $("#CategorySlt").val();
    var score = $("#ScoreTxt").val();
    var course = $("#CourseSlt").val();

    if (title == "" || category == "-1" || score == "" || isNaN(score) || course == "-1") {
        alert("请完整填写题目信息");
        err = true;
    }

    if (score == "0") {
        alert("分值不能为0");
        err = true;
    }

    var isall = true;
    var chkarr = [];

    for (var i = 0; i < count; i++) {
        if ($("#chb_" + i)[0].checked) {
            isall = false;
            chkarr.push(1);
        } else {
            chkarr.push(0);
        }
    }
    $("#chk_v").val(chkarr);
    //  alert($("#chk_v").val());

    if (category == 2 || category == 3) {
        if (isall) {
            alert("至少有一项为正确答案");
            err = true;
        }
    }

    var areaArr = [];
    var isChecked = false;
    var areaList = document.getElementsByName("AreaChb");
    for (var i = 0; i < areaList.length; i++) {
        if ($("#AreaChb_" + i)[0].checked) {
            isChecked = true;
            areaArr.push($("#AreaChb_" + i).val());
        }
    }

    if (!isChecked) {
        alert("请至少选择一个地区");
        err = true;
    }

    $("#area_v").val(areaArr);

    if (err) {
        return false;
    } else {
        return true;
    }
}

function DeleteSelection() {
    if (confirm("确定要删除所选项目吗？")) {
        var arr = [];
        var chblist = document.getElementsByName("p_chkb");
        for (var i = 0; i < chblist.length; i++) {
            if (chblist[i].checked) {
                arr.push(chblist[i].value);
            }
        }
        if (arr.length == 0) {
            alert("未选择任何项目吗");
        } else {
            document.getElementById("arrv").value = arr;
            document.getElementById("form1").action = "/exam/QuestionDelete.aspx?act=d";
            document.getElementById("form1").submit();
        }
    }
}


function DeleteSelection_D() {
    if (confirm("确定要删除所选项目吗？")) {
        var arr = [];
        var chblist = document.getElementsByName("p_chkb");
        for (var i = 0; i < chblist.length; i++) {
            if (chblist[i].checked) {
                arr.push(chblist[i].value);
            }
        }
        if (arr.length == 0) {
            alert("未选择任何项目吗");
        } else {
            document.getElementById("arrv").value = arr;
            document.getElementById("form1").action = "/exam/ExamDefaultSetDel.aspx?act=d";
            document.getElementById("form1").submit();
        }
    }
}

function DeleteSelection_E() {
    if (confirm("确定要删除所选项目吗？")) {
        var arr = [];
        var chblist = document.getElementsByName("p_chkb");
        for (var i = 0; i < chblist.length; i++) {
            if (chblist[i].checked) {
                arr.push(chblist[i].value);
            }
        }
        if (arr.length == 0) {
            alert("未选择任何项目吗");
        } else {
            document.getElementById("arrv").value = arr;
            document.getElementById("form1").action = "/exam/ExamDelete.aspx?act=d";
            document.getElementById("form1").submit();
        }
    }
}

function DeleteSelection_E1() {
    if (confirm("确定要删除所选项目吗？")) {
        var arr = [];
        var chblist = document.getElementsByName("p_chkb");
        for (var i = 0; i < chblist.length; i++) {
            if (chblist[i].checked) {
                arr.push(chblist[i].value);
            }
        }
        if (arr.length == 0) {
            alert("未选择任何项目吗");
        } else {
            document.getElementById("arrv").value = arr;
            document.getElementById("form1").action = "/internals/exam/D.aspx?act=d";
            document.getElementById("form1").submit();
        }
    }
}

function DeleteSelection_E2() {
    if (confirm("确定要删除所选项目吗？")) {
        var arr = [];
        var chblist = document.getElementsByName("p_chkb");
        for (var i = 0; i < chblist.length; i++) {
            if (chblist[i].checked) {
                arr.push(chblist[i].value);
            }
        }
        if (arr.length == 0) {
            alert("未选择任何项目吗");
        } else {
            document.getElementById("arrv").value = arr;
            document.getElementById("form1").action = "/internals/publics/exam/D.aspx?act=d";
            document.getElementById("form1").submit();
        }
    }
}

function QuestionListExport() {
    document.getElementById("form1").action = "/exam/QuestionExport.aspx";
    document.getElementById("form1").submit();
}

function QuestionListSearch() {
    document.getElementById("form1").action = "/exam/QuestionList.aspx?act=s";
    document.getElementById("form1").submit();
}

//function GetRelationContent(v) {
//  ajaxRead("/tools/ajax.aspx?t=getRelationContent&v=" + v, "DrawRelationContent(obj);");
//}

//function DrawRelationContent(obj) {
//  var res = obj.getElementsByTagName("result");
//  var resContainer = document.getElementById("RelationSlt");
//  resContainer.length = 0;
//  resContainer.options.add(new Option("请选择课程", "-1"));
//  var result = "";
//  if (res[0] != null && res[0] != undefined) {
//    if (res[0].childNodes.length > 1) {
//      for (var i = 0; i < res[0].childNodes.length; i++) {
//        if (res[0].childNodes[i].nodeName == "#text") {
//          i = i + 1;
//        }
//        var relationid = res[0].childNodes[i].attributes[0].value;
//        var relationvalue = res[0].childNodes[i].attributes[1].value;
//        resContainer.options.add(new Option(relationvalue, relationid));
//      }
//    } else {
//      if (res[0].childNodes[0] != null) {
//        var relationid = res[0].childNodes[0].attributes[0].value;
//        var relationvalue = res[0].childNodes[0].attributes[1].value;
//        resContainer.options.add(new Option(relationvalue, relationid));
//      }
//    }
//  }
//}

//function GetRelationContentWithSetValue(v,v2) {
//  ajaxRead("/tools/ajax.aspx?t=getRelationContent&v=" + v, "DrawRelationContentWithSetValue(obj," + v2 + ");");
//}

//function DrawRelationContentWithSetValue(obj,v) {
//  var res = obj.getElementsByTagName("result");
//  var resContainer = document.getElementById("RelationSlt");
//  resContainer.length = 0;
//  resContainer.options.add(new Option("请选择课程", "-1"));
//  var result = "";
//  if (res[0] != null && res[0] != undefined) {
//    if (res[0].childNodes.length > 1) {
//      for (var i = 0; i < res[0].childNodes.length; i++) {
//        if (res[0].childNodes[i].nodeName == "#text") {
//          i = i + 1;
//        }
//        var relationid = res[0].childNodes[i].attributes[0].value;
//        var relationvalue = res[0].childNodes[i].attributes[1].value;
//        resContainer.options.add(new Option(relationvalue, relationid));
//      }
//    } else {
//      if (res[0].childNodes[0] != null) {
//        var relationid = res[0].childNodes[0].attributes[0].value;
//        var relationvalue = res[0].childNodes[0].attributes[1].value;
//        resContainer.options.add(new Option(relationvalue, relationid));
//      }
//    }
//  }
//  if (v != "")
//    resContainer.value = v;
//}


function CheckExamForm() {
    var title = $("#TitleTxt").val();
    var category = $("#ExamCategorySlt").val();
    //  var relation = $("#RelationSlt").val();
    var certificateid = $("#CertificateSlt").val();
    var courseid = $("#CourseSlt").val();
    var starttime = $("#StartTimeTxt").val();
    var endtime = $("#EndTimeTxt").val();
    var examtime = $("#ExamTimeTxt").val();
    var totalscore = $("#TotalScoreTxt").val();
    var passscore = $("#PassScoreTxt").val();
    var questiontype = $("input[name='rQuestionStyle']:checked").val();
    var err = false;

    var areaArr = [];
    var isChecked = false;
    var areaList = document.getElementsByName("AreaChb");
    for (var i = 0; i < areaList.length; i++) {
        if ($("#AreaChb_" + i)[0].checked) {
            isChecked = true;
            areaArr.push($("#AreaChb_" + i).val());
        }
    }

    if (!isChecked) {
        alert("请至少选择一个地区");
        err = true;
    }

    $("#area_v").val(areaArr);

    switch (parseInt(category)) {
        case -1:
            alert("请选择试卷类型");
            err = true;
            break;
        case 1:
            if (parseInt(certificateid) == -1 || parseInt(courseid == -1)) {
                alert("请选择课程及认证");
                err = true;
            } else {
                $("#coursevalue").val(courseid);
            }
            break;
        case 2:
            if (parseInt(certificateid) == -1) {
                alert("请选择认证类别");
                err = true;
            }
            break;
        default:
            alert("试卷类型选择错误");
            err = true;
            break;
    }

    if (title == "" || category == "-1" || isNaN(examtime) || starttime == "" || endtime == "") {
        alert("请完整填写试卷信息");
        err = true;
    }

    if (totalscore == "" || isNaN(totalscore)) {
        alert("分数不能为空或只能为数字");
        err = true;
    }

    if (passscore == "" || isNaN(passscore)) {
        alert("分数不能为空或只能为数字");
        err = true;
    }

    if (!err) {
        if (questiontype == 1) {
            err = CheckSettingValue();
        }
        else {
            err = CheckSettingQuestionValue();
        }
    }


    if (err) {
        return false;
    } else {
        return true;
    }
}




function GetEmptySettingTemplate() {
    var categoryid = parseInt($("#ExamCategorySlt").val());
    var certificateid = parseInt($("#CertificateSlt").val());
    var courseid = parseInt($("#CourseSlt").val());
    var link = document.getElementById("defaultlink");
    link.style.display = "none";
    GetEmptySettingTemplateBase(categoryid, certificateid, courseid);
}

function GetEmptySettingTemplate2(cid) {
    var categoryid = cid;
    var certificateid = parseInt($("#CertificateSlt").val());
    var courseid = parseInt($("#CourseSlt").val());

    GetEmptySettingTemplateBase(categoryid, certificateid, courseid);
}
function SetDisplayExamSetting(divid, imgid) {

    if ($("#" + divid).css("display") == "none") {
        $("#" + imgid).attr("src", "../../images/icon/arrow_down.gif");
        $("#" + divid).show("fast");
    }
    else {
        $("#" + imgid).attr("src", "../../images/icon/arrow_right.gif");
        $("#" + divid).hide("slow");
    }
}
function GetEmptySettingTemplateBase(categoryid, certificateid, courseid) {

    switch (categoryid) {
        case -1:
            $("#SettingTemplateDiv").html("");
            RemoveSelectOptions("CourseSlt");
            break;
        case 1:
            $("#SettingTemplateDiv").html("");
            if (certificateid != -1) {
                if ($("#act").val() == "u") {
                    var questiontype = $("input[name='rQuestionStyle']:checked").val();
                    var questionIDArr = $("#valueQuestionArr").val();
                    ajaxRead("/tools/ajax.aspx?t=getEmptySettingTemplate&cid=" + categoryid + "&courseid=" + courseid + "&isquestionrandom=" + questiontype + "&questionIDArr=" + questionIDArr, "DrawEmptySettingTemplate(obj);");
                }
                GetCourseList(certificateid);
            }
            break;
        case 2:

            if (certificateid != -1) {
                GetCertificateEmptySettingTemplate(certificateid, categoryid);
            }
            RemoveSelectOptions("CourseSlt");
            break;
        default:
            $("#SettingTemplateDiv").html("");
            RemoveSelectOptions("CourseSlt");
            break;
    }
}



function T() {
    var categoryid = $("#ExamCategorySlt").val();
    var courseid = $("#CourseSlt").val();
    var questiontype = $("input[name='rQuestionStyle']:checked").val();

    if (courseid != "-1") {
        if (questiontype == "1") {

            ajaxRead("/tools/ajax.aspx?t=getEmptySettingTemplate&cid=" + categoryid + "&courseid=" + courseid + "&isquestionrandom=1", "DrawEmptySettingTemplate(obj);");
        }
        else {
            ajaxRead("/tools/ajax.aspx?t=getEmptySettingTemplate&cid=" + categoryid + "&courseid=" + courseid + "&isquestionrandom=0", "DrawEmptySettingTemplate(obj);");
        }
    }
}

function DrawEmptySettingTemplate(obj) {
    var res = obj.getElementsByTagName("result");
    var result = "";
    if (res[0] != null && res[0] != undefined) {
        if (res[0].childNodes.length > 0) {
            result = res[0].childNodes[0].childNodes[0].nodeValue;
            var questiontype = $("input[name='rQuestionStyle']:checked").val();
            var link = document.getElementById("defaultlink");
            $("#DefaultSlt").val("-1");
            if (questiontype == "1") {
                link.style.display = "";
            }
            else {
                link.style.display = "none";
            }

            $("#SettingTemplateDiv").html(result);
        } else {
            $("#SettingTemplateDiv").html("");
        }
    } else {
        $("#SettingTemplateDiv").html("");
    }
}

function GetCertificateEmptySettingTemplate(id, cid) {
    var questiontype = $("input[name='rQuestionStyle']:checked").val();
    var questionIDArr = $("#valueQuestionArr").val();
    ajaxRead("/tools/ajax.aspx?t=getCertificateSettingTemplate&cid=" + cid + "&id=" + id + "&isquestionrandom=" + questiontype + "&questionIDArr=" + questionIDArr, "DrawEmptySettingTemplate(obj);");
}
function CheckSettingQuestionValue() {
    var err = false;
    var categoryValue = $("#categoryvalue").val();
    var courseValue = $("#coursevalue").val();
    var questionIDValue = $("#questionidvalue").val();
    var valueArr = [];

    if (questionIDValue == undefined || courseValue == undefined) {
        err = true;
    } else {
        var categoryArr = categoryValue.split(",");
        var courseArr = courseValue.split(",");


        if (courseArr.length == 1) {

            var questionIDArr = questionIDValue.split(",");
            for (var i = 0; i < questionIDArr.length; i++) {
                if (questionIDArr[i] != "") {

                    var chkName = "chk_" + questionIDArr[i];
                    $("input[name=" + chkName + "]:checked").each(function () {

                        valueArr.push(questionIDArr[i]);
                    });

                }
            }
            $("#valueQuestionArr").val(valueArr);
        } else {
            var questionCourseArr = questionIDValue.split(";");
            for (var i = 0; i < questionCourseArr.length; i++) {
                if (questionCourseArr[i] != "") {

                    var questionIDArr = questionCourseArr[i].split(",");
                    for (var j = 0; j < questionIDArr.length; j++) {
                        if (questionIDArr[j] != "") {

                            var chkName = "chk_" + questionIDArr[j];


                            $("input[name=" + chkName + "]:checked").each(function () {

                                valueArr.push(questionIDArr[j]);

                            });

                        }
                    }
                }
            }

        }
    }
    $("#valueQuestionArr").val(valueArr);
    if (err) {
        alert("填写数据有误");
        return true;
    } else {
        $("#valueQuestionArr").val(valueArr);
        return false;
    }
}
function CheckSettingValue() {
    var err = false;
    var categoryValue = $("#categoryvalue").val();
    var courseValue = $("#coursevalue").val();

    if (categoryValue == undefined || courseValue == undefined) {
        err = true;
    } else {
        var categoryArr = categoryValue.split(",");
        var courseArr = courseValue.split(",");
        if (courseArr.length == 1) {
            var valueArr = [];
            var valueArr_re = [];
            var qty_total = 0;
            for (var i = 0; i < categoryArr.length; i++) {
                var v = $("#input_" + categoryArr[i]).val();
                var v_re = $("#input_" + categoryArr[i] + "_re").val();
                var q = $("#qty_" + categoryArr[i]).val();
                var q_re = $("#qty_" + categoryArr[i] + "_re").val();
                if (v == "" || isNaN(v) || v < 0 || v_re == "" || isNaN(v_re) || v_re < 0 || v > q || v_re > q_re) {
                    err = true;
                    break;
                }
                if (err) {
                    break;
                }
                qty_total = qty_total + v + v_re;
                valueArr.push(v);
                valueArr_re.push(v_re);
            }
            if (qty_total <= 0) {
                err = true;
            }


            $("#inputvalue").val(valueArr);
            $("#inputvalue_re").val(valueArr_re);
        } else {
            var valueArr = "";
            for (var i = 0; i < categoryArr.length; i++) {
                var subValueArr = [];
                var subValueArr_re = [];
                for (var j = 0; j < courseArr.length; j++) {
                    var v = $("#input_" + categoryArr[i] + "_" + courseArr[j]).val();
                    var v_re = $("#input_" + categoryArr[i] + "_" + courseArr[j] + "_re").val();
                    if (v == "" || isNaN(v) || v <= 0 || v_re == "" || isNaN(v_re) || v_re <= 0) {
                        err = true;
                        break;
                    }
                    subValueArr.push(v);
                    subValueArr_re.push(v_re);
                }
                if (err) {
                    break;
                }
                valueArr += subValueArr;
                valueArr_re += subValueArr_re;
                if (i < categoryArr.length - 1) {
                    valueArr += "|";
                    valueArr_re += "|";
                }
            }
        }
    }

    if (err) {
        alert("填写数据有误");
        return true;
    } else {
        $("#inputvalue").val(valueArr);
        $("#inputvalue_re").val(valueArr_re);
        return false;
    }
}

function GetTestValue() {
    var categoryValue = $("#categoryvalue").val();
    var courseValue = $("#coursevalue").val();

    //  alert("categoryValue：" + categoryValue);
    //  alert("courseValue：" + courseValue);

    var categoryArr = categoryValue.split(",");
    var courseArr = courseValue.split(",");
    if (courseArr.length == 1) {
        var valueArr = [];
        for (var i = 0; i < categoryArr.length; i++) {
            var v = $("#input_" + categoryArr[i]).val();
            if (v == "" || isNaN(v) || v <= 0) {
                alert("填写数据有误");
                return;
            }
            valueArr.push(v);
        }
        $("#inputvalue").val(valueArr);
    } else {
        var valueArr = "";
        for (var i = 0; i < categoryArr.length; i++) {
            var subValueArr = [];
            for (var j = 0; j < courseArr.length; j++) {
                var v = $("#input_" + categoryArr[i] + "_" + courseArr[j]).val();
                if (v == "" || isNaN(v) || v <= 0) {
                    alert("填写数据有误");
                    return;
                }
                subValueArr.push(v);
            }
            valueArr += subValueArr;
            if (i < categoryArr.length - 1) {
                valueArr += "|";
            }
        }
        $("#inputvalue").val(valueArr);
    }

    //  alert($("#inputvalue").val());
}

function SetSettingValue(categoryid, certificateid, courseid) {
    var err = false;
    switch (parseInt(categoryid)) {
        case 1:
            //GetEmptySettingTemplateBase(categoryid, certificateid, courseid);
            setTimeout(function () { $("#coursevalue").val(certificateid); }, 300);
            break;
        case 2:
            GetCertificateEmptySettingTemplate(certificateid, categoryid);
            break;
        default:
            err = true;
            break;
    }

    if (!err) {
        setTimeout(SetSettingLoadValue, 700);
    }
}

function defaultS() {
        var defaultvalue = $("#DefaultSlt").val();

        var target = defaultvalue.split("|")[0];
        var value = defaultvalue.split("|")[1];
        var value_re = defaultvalue.split("|")[2];

        var targetArr = [];
        var valueArr = [];
        var valueArr_re = [];

        if (target != "" && value != "" && target.indexOf(",") != -1 && value.indexOf(",") != -1 && value_re != "" && value_re.indexOf(",") != -1) {
            targetArr = target.split(",");
            valueArr = value.split(",");
            valueArr_re = value_re.split(",");
            for (var i = 0; i < targetArr.length; i++) {
                $("#input_" + targetArr[i]).val(valueArr[i]);
                $("#input_" + targetArr[i] + "_re").val(valueArr_re[i]);
            }
        }

}

function SetSettingLoadValue() {
    var target = $("#targetArr").val();
    var value = $("#valueArr").val();
    var value_re = $("#valueArr_re").val();

    var targetArr = [];
    var valueArr = [];
    var valueArr_re = [];

    if (target != "" && value != "" && target.indexOf(",") != -1 && value.indexOf(",") != -1 && value_re != "" && value_re.indexOf(",") != -1) {
        targetArr = target.split(",");
        valueArr = value.split(",");
        valueArr_re = value_re.split(",");
        for (var i = 0; i < targetArr.length; i++) {
            $("#input_" + targetArr[i]).val(valueArr[i]);
            $("#input_" + targetArr[i] + "_re").val(valueArr_re[i]);
        }
    }
}


function GetCourseList(v) {
    ajaxRead("/tools/ajax.aspx?t=getCourseListWithCourseID&cid=" + v, "DrawCourseList(obj);");
}

function DrawCourseList(obj) {
    var res = obj.getElementsByTagName("result");
    var resContainer = document.getElementById("CourseSlt");
    resContainer.length = 0;
    resContainer.options.add(new Option("请选择课程", "-1"));
    var result = "";
    if (res[0] != null && res[0] != undefined) {
        if (res[0].childNodes.length > 1) {
            for (var i = 0; i < res[0].childNodes.length; i++) {
                if (res[0].childNodes[i].nodeName == "#text") {
                    i = i + 1;
                }
                var id = res[0].childNodes[i].attributes[0].value;
                var name = res[0].childNodes[i].attributes[1].value;
                resContainer.options.add(new Option(name, id));
            }
        } else {
            if (res[0].childNodes[0] != null) {
                var id = res[0].childNodes[0].attributes[0].value;
                var name = res[0].childNodes[0].attributes[1].value;
                resContainer.options.add(new Option(name, id));
            }
        }
    }
}



function RemoveSelectOptions(target) {
    var slt = document.getElementById(target);
    //  for (var i = 0; i < slt.options.length; i++) {
    //    slt.options.remove(i);
    //  }
    slt.options.length = 0;
    slt.options.add(new Option("请选择课程", "-1"));
}

function GetTotalScore() {
    var categorySlt = parseInt($("#ExamCategorySlt").val());
    var tips = "";
    var err = false;
    switch (categorySlt) {
        case -1:
            tips = "请选择试卷类型";
            err = true;
            break;
        case 1:
            if ($("#CertificateSlt").val() == -1 || $("#CourseSlt").val() == -1) {
                tips = "请选择认证类别及课程";
                err = true;
            }
            break;
        case 2:
            if ($("#CertificateSlt").val() == -1) {
                tips = "请选择认证类别";
                err = true;
            }
            break;
        default:
            tips = "请求错误";
            err = true;
            break;
    }

    if (!err) {
        var categoryarr = [];
        var coursearr = [];
        categoryarr = $("#categoryvalue").val().split(",");
        if ($("#coursevalue").val().indexOf(",") != -1) {
            coursearr = $("#coursevalue").val().split(",");
        } else {
            coursearr[0] = $("#coursevalue").val();
        }

        var total = [];
        if (categorySlt == 1) {
            for (var i = 0; i < categoryarr.length; i++) {
                var categoryQuantity = 0;
                categoryQuantity += parseInt($("#input_" + categoryarr[i]).val() == "" ? 0 : parseInt($("#input_" + categoryarr[i]).val())) + parseInt($("#input_" + categoryarr[i] + "_re").val() == "" ? 0 : parseInt($("#input_" + categoryarr[i] + "_re").val()));
                total.push(categoryQuantity);
            }
        } else {
            for (var i = 0; i < categoryarr.length; i++) {
                var categoryQuantity = 0;
                for (var j = 0; j < coursearr.length; j++) {
                    var count = parseInt($("#input_" + categoryarr[i] + "_" + coursearr[j]).val() == "" ? 0 : parseInt($("#input_" + categoryarr[i] + "_" + coursearr[j]).val())) + parseInt($("#input_" + categoryarr[i] + "_" + coursearr[j] + "_re").val() == "" ? 0 : parseInt($("#input_" + categoryarr[i] + "_" + coursearr[j] + "_re").val()));
                    categoryQuantity += count;
                }
                total.push(categoryQuantity);
            }
        }

        ajaxRead("/tools/ajax.aspx?t=getTotalScore&cid=" + categoryarr + "&quantity=" + total, "DrawScore(obj);");
    } else {
        alert(tips);
    }
}

function DrawScore(obj) {
    var res = obj.getElementsByTagName("result");
    var result = "";
    var examTotalScore = 0;
    if (res[0] != null && res[0] != undefined) {
        if (res[0].childNodes.length > 1) {
            for (var i = 0; i < res[0].childNodes.length; i++) {
                if (res[0].childNodes[i].nodeName == "#text") {
                    i = i + 1;
                }
                var id = res[0].childNodes[i].attributes[0].value;
                var total = res[0].childNodes[i].attributes[1].value;
                var per = res[0].childNodes[i].attributes[2].value;
                $("#total_" + id).html("&nbsp;&nbsp;总分：" + total + "&nbsp;每题：" + per);
                examTotalScore += parseInt(total);
            }
        } else {
            if (res[0].childNodes[0] != null) {
                var id = res[0].childNodes[0].attributes[0].value;
                var total = res[0].childNodes[0].attributes[1].value;
                var per = res[0].childNodes[0].attributes[2].value;
                $("#total_" + id).html("&nbsp;&nbsp;总分：" + total + "&nbsp;每题：" + per);
                examTotalScore += parseInt(total);
            }
        }
        //    if (res[0].childNodes[0] != null) {
        //      result = res[0].childNodes[0].nodeValue;
        $("#TotalScoreLb").html(examTotalScore);
        //    }
    }
}

function ConfirmSearch() {
    var certificateSlt = $("#CertificateSlt").val();
    var areaSlt = $("#AreaSlt").val();
    var scoreSlt = $("#ScoreSlt").val();
    var loginid = $("#LoginIDTxt").val();
    var userName = $("#UserNameTxt").val();
    var isInputedSlt = $("#IsInputedSlt").val();
    var quantity = $("#DisplayQuantitySlt").val();
    if (certificateSlt == "-1" && areaSlt == "-1" && scoreSlt == "-1" && loginid == "" && userName == "" && isInputedSlt == "-1" && quantity == "-1") {
        return (confirm("请尽可能的减少搜索范围，以减少查询所需要的时间。如果不选择任何搜索条件，查询将需要相对比较长的时间"));
    } else {
        return true;
    }
}

function InputHandle() {
    var count = parseInt($("#count").val());
    var uidArr = [];
    var scidArr = [];
    var sArr = [];

    var s_err = false;

    for (var i = 0; i < count; i++) {
        var uid = $("#UserID_" + i).val();
        var scid = parseInt($("#ScoreSlt_" + i).val());
        var s = $("#ScoreInput_" + i).val();
        if (isNaN(s)) {
            s_err = true;
            break;
        }
        if (s == "" || parseInt(s) > 100 || parseInt(s) < 0) {
            s_err = true;
            break;
        }
        if (scid != -1) {
            uidArr.push(uid);
            scidArr.push(scid);
            sArr.push(parseInt(s));
        }
    }

    if (s_err) {
        alert("成绩输入错误");
        return;
    }

    if (uidArr.length > 0 && scidArr.length > 0 && sArr.length > 0) {
        var repeat = false;
        for (var i = 0; i < uidArr.length; i++) {
            for (var j = 0; j < uidArr.length; j++) {
                if (i != j) {
                    if (uidArr[i] == uidArr[j] && scidArr[i] == scidArr[j]) {
                        repeat = true;
                        break;
                    }
                }
            }
            if (repeat) break;
        }

        if (repeat) {
            alert("输入数据有重复，请确认");
            return;
        } else {
            $("#uid_v").val(uidArr);
            $("#scid_v").val(scidArr);
            $("#s_v").val(sArr);

            var form = document.getElementById("form1");
            form.action = "/exam/InputHandle.aspx";
            form.submit();
        }
    } else {
        alert("数据填写有误");
        return;
    }
}

function CheckEditInputForm() {
    var count = parseInt($("#count").val());
    var uid = parseInt($("#id").val());
    var uidArr = [];
    var scidArr = [];
    var sArr = [];

    var s_err = false;

    for (var i = 0; i < count; i++) {
        var scid = parseInt($("#Category_" + i).val());
        var s = $("#Score_" + i).val();
        if (isNaN(s)) {
            s_err = true;
            break;
        }
        if (s == "" || parseInt(s) > 100 || parseInt(s) < 0) {
            s_err = true;
            break;
        }
        if (scid != -1 && s != "") {
            uidArr.push(uid);
            scidArr.push(scid);
            sArr.push(parseInt(s));
        }
    }

    if (s_err) {
        alert("成绩输入错误");
        return false;
    } else {
        $("#uid_v").val(uidArr);
        $("#scid_v").val(scidArr);
        $("#s_v").val(sArr);
        return true;
    }
}



function CheckExamDefault() {
    var title = $("#TitleTxt").val();
    var err = false;
    if (title == "") {
        alert("请完整填写名称");
        return err;
    }
    var categoryValue = $("#categoryvalue").val();
    if (categoryValue == undefined) {
        err = true;
    } else {
        var categoryArr = categoryValue.split(",");

            var valueArr = [];
            var valueArr_re = [];
            for (var i = 0; i < categoryArr.length; i++) {
                var v = $("#input_" + categoryArr[i]).val();
                var v_re = $("#input_" + categoryArr[i] + "_re").val();
                if (v == "" || isNaN(v) || v < 0 || v_re == "" || isNaN(v_re) || v_re < 0) {
                    err = true;
                    break;
                }
                if (err) {
                    break;
                }
                valueArr.push(v);
                valueArr_re.push(v_re);
            }
            $("#inputvalue").val(valueArr);
            $("#inputvalue_re").val(valueArr_re);
        

    } 
    if (err) {
        alert("填写数据有误");
        return false;
    } else {
        $("#inputvalue").val(valueArr);
        $("#inputvalue_re").val(valueArr_re);
        return true;
    }

}
